#!/bin/bash -e

# system channel name defaults to "my-sys-channel"
SYS_CHANNEL="my-sys-channel"
# channel name defaults to "mychannel"
CHANNEL_1_NAME="mychannelone"
CHANNEL_2_NAME="mychanneltwo"

# clean old files
rm -rf channel-artifacts/* mychannel.block crypto-config

# generate crypto
# generate certificate configuration
cryptogen generate --config=./crypto-config.yaml
# generate genesis block
configtxgen -profile FourOrgsOrdererGenesis -channelID $SYS_CHANNEL \
-outputBlock ./channel-artifacts/genesis.block

# generate channel configuration for channel_1
configtxgen -profile FourOrgsChannel \
-outputCreateChannelTx ./channel-artifacts/${CHANNEL_1_NAME}.tx \
-channelID $CHANNEL_1_NAME
# generate channel configuration for channel_2
configtxgen -profile FourOrgsChannel \
-outputCreateChannelTx ./channel-artifacts/${CHANNEL_2_NAME}.tx \
-channelID $CHANNEL_2_NAME

# configtxgen -profile FourOrgsChannel \
# -outputCreateChannelTx ./channel-artifacts/channel.tx \
# -channelID mychannel
# generate org{1..4}'s anchors
# configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate \
# ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
# configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate \
# ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
# configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate \
# ./channel-artifacts/Org3MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org3MSP
# configtxgen -profile FourOrgsChannel -outputAnchorPeersUpdate \
# ./channel-artifacts/Org4MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org4MSP
for i in {1..4}
do
    configtxgen -profile FourOrgsChannel \
    -outputAnchorPeersUpdate ./channel-artifacts/Org${i}MSPanchors_${CHANNEL_1_NAME}.tx \
    -channelID $CHANNEL_1_NAME -asOrg Org${i}MSP
done

for i in {1..4}
do
    configtxgen -profile FourOrgsChannel \
    -outputAnchorPeersUpdate ./channel-artifacts/Org${i}MSPanchors_${CHANNEL_2_NAME}.tx \
    -channelID $CHANNEL_2_NAME -asOrg Org${i}MSP
done

git add .
git commit -m "update crypto files"
git push